home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr29 / memsize.zip / DEBUG.CPP < prev    next >
Text File  |  1995-01-04  |  17KB  |  451 lines

  1. /****************************************************************** DEBUG.CPP
  2.  *                                                                          *
  3.  *  Debugging Aids                                                          *
  4.  *                                                                          *
  5.  ****************************************************************************/
  6.  
  7. #define INCL_BASE
  8. #define INCL_WIN
  9. #include <os2.h>
  10.  
  11. #include <stdarg.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <time.h>
  16.  
  17. #include "debug.h"
  18. #include "memsize.h"
  19. #include "mutex.h"
  20. #include "restring.h"
  21.  
  22. extern HFILE Timer = 0 ;
  23. extern BOOL Trace = FALSE ;
  24.  
  25.  
  26. /****************************************************************************
  27.  *                                                                          *
  28.  *                       Display Debug Message                              *
  29.  *                                                                          *
  30.  ****************************************************************************/
  31.  
  32. extern VOID Debug ( HWND hwnd, char *Message, ... )
  33. {
  34.  /***************************************************************************
  35.   * Local Declarations                                                      *
  36.   ***************************************************************************/
  37.  
  38.   va_list Marker ;
  39.   char Text [500] ;
  40.  
  41.  /***************************************************************************
  42.   * Format the debug message.                                               *
  43.   ***************************************************************************/
  44.  
  45.   va_start ( Marker, Message ) ;
  46.   vsprintf ( Text, Message, Marker ) ;
  47.   va_end ( Marker ) ;
  48.  
  49.  /***************************************************************************
  50.   * Display the log message and wait for the user to press ENTER.           *
  51.   ***************************************************************************/
  52.  
  53.   WinMessageBox ( HWND_DESKTOP, hwnd, PSZ(Text), PSZ("Debug"), 0, MB_ENTER ) ;
  54. }
  55.  
  56. /****************************************************************************
  57.  *                                                                          *
  58.  *                         Log Debug Message                                *
  59.  *                                                                          *
  60.  ****************************************************************************/
  61.  
  62. Mutex LogSemaphore ( PSZ(NULL) ) ;
  63.  
  64. extern VOID Log ( char *Message, ... )
  65. {
  66.  /***************************************************************************
  67.   * Serialize this function by requesting its semaphore.                    *
  68.   ***************************************************************************/
  69.  
  70.   LogSemaphore.Request ( SEM_INDEFINITE_WAIT ) ;
  71.  
  72.  /***************************************************************************
  73.   * Format the message.                                                     *
  74.   ***************************************************************************/
  75.  
  76.   va_list Marker ;
  77.   va_start ( Marker, Message ) ;
  78.  
  79.   static char Buffer [1024] ;
  80.   vsprintf ( Buffer, Message, Marker ) ;
  81.  
  82.   va_end ( Marker ) ;
  83.  
  84.  /***************************************************************************
  85.   * Open the log file.                                                      *
  86.   ***************************************************************************/
  87.  
  88.   FILE *File = fopen ( "MEMSIZE.LOG", "a" ) ;
  89.  
  90.  /***************************************************************************
  91.   * If the file got opened, write the message to the log file and close it. *
  92.   ***************************************************************************/
  93.  
  94.   if ( File ) {
  95.     char Time [9], Date [9] ;
  96.     fprintf ( File, "%s %s %s\n", _strtime(Time), _strdate(Date), Buffer ) ;
  97.     fclose ( File ) ;
  98.   } /* endif */
  99.  
  100.  /***************************************************************************
  101.   * Release the function semaphore and return.                              *
  102.   ***************************************************************************/
  103.  
  104.   LogSemaphore.Release ( ) ;
  105. }
  106.  
  107. /****************************************************************************
  108.  *                                                                          *
  109.  *                          Open Timer for Use                              *
  110.  *                                                                          *
  111.  ****************************************************************************/
  112.  
  113. extern BOOL OpenTimer ( VOID )
  114. {
  115.   if ( Timer )
  116.     DosClose ( Timer ) ;
  117.  
  118.   ULONG Action ;
  119.   if ( DosOpen ( (PSZ)"TIMER$", &Timer, &Action, 0, FILE_NORMAL, FILE_OPEN, OPEN_SHARE_DENYNONE, 0 ) )
  120.   {
  121.     return ( FALSE ) ;
  122.   }
  123.  
  124.   return ( TRUE ) ;
  125. }
  126.  
  127. /****************************************************************************
  128.  *                                                                          *
  129.  *                              Close Timer                                 *
  130.  *                                                                          *
  131.  ****************************************************************************/
  132.  
  133. extern VOID CloseTimer ( VOID )
  134. {
  135.   DosClose ( Timer ) ;
  136. }
  137.  
  138. /****************************************************************************
  139.  *                                                                          *
  140.  *                       Read Time from HRTIMER.SYS                         *
  141.  *                                                                          *
  142.  ****************************************************************************/
  143.  
  144. extern BOOL GetTime ( PTIMESTAMP pts )
  145. {
  146.   ULONG ByteCount ;
  147.  
  148.   if ( DosRead ( Timer, pts, sizeof(*pts), &ByteCount ) )
  149.     return ( FALSE ) ;
  150.  
  151.   return ( TRUE ) ;
  152. }
  153.  
  154. /****************************************************************************
  155.  *                                                                          *
  156.  *                         Calculate Elapsed Time                           *
  157.  *                                                                          *
  158.  ****************************************************************************/
  159.  
  160. extern ULONG ComputeElapsedTime ( PTIMESTAMP ptsStart, PTIMESTAMP ptsStop, PULONG pulNs )
  161. {
  162.   ULONG ulMsecs, ulNsecs;
  163.   TIMESTAMP tsStart, tsStop ;
  164.  
  165.   tsStart = *ptsStart ;                       // De-reference timestamp
  166.                                               //     structures for speed
  167.   tsStop  = *ptsStop ;
  168.  
  169.   ulMsecs = tsStop.ulMs - tsStart.ulMs ;      // Elapsed milliseconds
  170.  
  171.   if( tsStart.ulNs > tsStop.ulNs )            // If nanosecond overflow ...
  172.   {
  173.     ulNsecs = (1000000 + tsStop.ulNs) - tsStart.ulNs; // Adjust nanoseconds
  174.     ulMsecs--;                                        // Adjust milliseconds
  175.   }
  176.   else
  177.     ulNsecs = tsStop.ulNs - tsStart.ulNs ;    // No overflow..Elapsed nanos
  178.  
  179.   *pulNs = ulNsecs ;
  180.  
  181.   return ( ulMsecs ) ;
  182. }
  183.  
  184. /****************************************************************************
  185.  *                                                                          *
  186.  *  Allocate Memory                                                         *
  187.  *                                                                          *
  188.  ****************************************************************************/
  189.  
  190. //#define ALLOCATE_THROUGH_DOS
  191.  
  192. extern PVOID AllocateMemory ( ULONG ByteCount )
  193. {
  194.   #ifdef ALLOCATE_THROUGH_DOS
  195.   {
  196.     PVOID Memory ;
  197.     DosAllocMem ( &Memory, ByteCount, PAG_READ | PAG_WRITE | PAG_COMMIT ) ;
  198.     return ( Memory ) ;
  199.   }
  200.   #else
  201.   {
  202.     return ( malloc ( ByteCount ) ) ;
  203.   }
  204.   #endif
  205. }
  206.  
  207. /****************************************************************************
  208.  *                                                                          *
  209.  *  Free Memory                                                             *
  210.  *                                                                          *
  211.  ****************************************************************************/
  212.  
  213. extern VOID FreeMemory ( PVOID Memory )
  214. {
  215.   #ifdef ALLOCATE_THROUGH_DOS
  216.   {
  217.     DosFreeMem ( Memory ) ;
  218.   }
  219.   #else
  220.   {
  221.     free ( Memory ) ;
  222.   }
  223.   #endif
  224. }
  225.  
  226. /***************************************************************************
  227.  *                                                                         *
  228.  *  Exception Handler                                                      *
  229.  *                                                                         *
  230.  ***************************************************************************/
  231.  
  232. extern ULONG APIENTRY ExceptionHandler
  233. (
  234.   PEXCEPTIONREPORTRECORD pExceptionReportRecord,
  235.   PEXCEPTIONREGISTRATIONRECORD pExceptionRegistrationRecord,
  236.   PCONTEXTRECORD pContextRecord,
  237.   PVOID pDispatcherContext
  238. )
  239. {
  240.    extern HMODULE Library ;
  241.  
  242.    if ( pExceptionReportRecord->ExceptionNum == XCPT_ACCESS_VIOLATION )
  243.    {
  244.       Log
  245.       (
  246.         "ABORT: %s%s%s%s%s%sviolation trying to access address/selector %08lX.\n"
  247.         "  The registers were as follows:\n"
  248.         "    AX:%08lX  BX:%08lX  CX:%08lX  DX:%08lX\n"
  249.         "    FL:%08lX  DI:%08lX  SI:%08lX  BP:%08lX\n"
  250.         "    CS:%08lX  IP:%08lX  SS:%08lX  SP:%08lX\n"
  251.         "    DS:%08lX  ES:%08lX  FS:%08lX  GS:%08lX",
  252.         ( pExceptionReportRecord->ExceptionInfo[0] & XCPT_READ_ACCESS ) ? "Read " : "",
  253.         ( pExceptionReportRecord->ExceptionInfo[0] & XCPT_WRITE_ACCESS ) ? "Write " : "",
  254.         ( pExceptionReportRecord->ExceptionInfo[0] & XCPT_EXECUTE_ACCESS ) ? "Execute " : "",
  255.         ( pExceptionReportRecord->ExceptionInfo[0] & XCPT_SPACE_ACCESS ) ? "Space " : "",
  256.         ( pExceptionReportRecord->ExceptionInfo[0] & XCPT_LIMIT_ACCESS ) ? "Limit " : "",
  257.         ( pExceptionReportRecord->ExceptionInfo[0] & XCPT_UNKNOWN_ACCESS ) ? "Unknown " : "",
  258.         pExceptionReportRecord->ExceptionInfo[1],
  259.         pContextRecord->ctx_RegEax, 
  260.         pContextRecord->ctx_RegEbx,
  261.         pContextRecord->ctx_RegEcx, 
  262.         pContextRecord->ctx_RegEdx,
  263.         pContextRecord->ctx_EFlags,
  264.         pContextRecord->ctx_RegEdi,
  265.         pContextRecord->ctx_RegEsi,
  266.         pContextRecord->ctx_RegEbp,
  267.         pContextRecord->ctx_SegCs,  
  268.         pContextRecord->ctx_RegEip,
  269.         pContextRecord->ctx_SegSs,
  270.         pContextRecord->ctx_RegEsp,
  271.         pContextRecord->ctx_SegDs,  
  272.         pContextRecord->ctx_SegEs,
  273.         pContextRecord->ctx_SegFs,  
  274.         pContextRecord->ctx_SegGs
  275.       ) ;
  276.  
  277.       PLONG Base = PLONG(pContextRecord->ctx_RegEbp) ;
  278.       while ( Base && ( *Base > LONG(Base) ) ) {
  279.         Log ( "  Calling function was at %08lX.", *(Base+1) ) ;
  280.         Base = PLONG(*Base) ;
  281.       } /* endwhile */
  282.                      
  283.       ResourceString Exception ( Library, IDS_EXCEPTION ) ;
  284.       Debug ( HWND_DESKTOP, PCHAR(Exception), _fullpath(PCHAR(NULL),"MEMSIZE.LOG",0) ) ;
  285.  
  286.       DosExit ( EXIT_PROCESS, 0 ) ;
  287.    }
  288.  
  289.    if ( pExceptionReportRecord->ExceptionNum == XCPT_PRIVILEGED_INSTRUCTION )
  290.    {
  291.       Log
  292.       (
  293.         "ABORT: A privileged instruction was encountered.\n"
  294.         "  The registers were as follows:\n"
  295.         "    AX:%08lX  BX:%08lX  CX:%08lX  DX:%08lX\n"
  296.         "    FL:%08lX  DI:%08lX  SI:%08lX  BP:%08lX\n"
  297.         "    CS:%08lX  IP:%08lX  SS:%08lX  SP:%08lX\n"
  298.         "    DS:%08lX  ES:%08lX  FS:%08lX  GS:%08lX",
  299.         pContextRecord->ctx_RegEax,
  300.         pContextRecord->ctx_RegEbx,
  301.         pContextRecord->ctx_RegEcx,
  302.         pContextRecord->ctx_RegEdx,
  303.         pContextRecord->ctx_EFlags,
  304.         pContextRecord->ctx_RegEdi,
  305.         pContextRecord->ctx_RegEsi,
  306.         pContextRecord->ctx_RegEbp,
  307.         pContextRecord->ctx_SegCs,
  308.         pContextRecord->ctx_RegEip,
  309.         pContextRecord->ctx_SegSs,
  310.         pContextRecord->ctx_RegEsp,
  311.         pContextRecord->ctx_SegDs,
  312.         pContextRecord->ctx_SegEs,
  313.         pContextRecord->ctx_SegFs,
  314.         pContextRecord->ctx_SegGs
  315.       ) ;
  316.                      
  317.       PLONG Base = PLONG(pContextRecord->ctx_RegEbp) ;
  318.       while ( Base && ( *Base > LONG(Base) ) ) {
  319.         Log ( "  Calling function was at %08lX.", *(Base+1) ) ;
  320.         Base = PLONG(*Base) ;
  321.       } /* endwhile */
  322.  
  323.       ResourceString Exception ( Library, IDS_EXCEPTION ) ;
  324.       Debug ( HWND_DESKTOP, PCHAR(Exception), _fullpath(PCHAR(NULL),"MEMSIZE.LOG",0) ) ;
  325.  
  326.       DosExit ( EXIT_PROCESS, 0 ) ;
  327.    }
  328.  
  329.    if ( pExceptionReportRecord->ExceptionNum == XCPT_ILLEGAL_INSTRUCTION )
  330.    {
  331.       Log
  332.       (
  333.         "ABORT: An illegal instruction was encountered.\n"
  334.         "  The registers were as follows:\n"
  335.         "    AX:%08lX  BX:%08lX  CX:%08lX  DX:%08lX\n"
  336.         "    FL:%08lX  DI:%08lX  SI:%08lX  BP:%08lX\n"
  337.         "    CS:%08lX  IP:%08lX  SS:%08lX  SP:%08lX\n"
  338.         "    DS:%08lX  ES:%08lX  FS:%08lX  GS:%08lX",
  339.         pContextRecord->ctx_RegEax,
  340.         pContextRecord->ctx_RegEbx,
  341.         pContextRecord->ctx_RegEcx,
  342.         pContextRecord->ctx_RegEdx,
  343.         pContextRecord->ctx_EFlags,
  344.         pContextRecord->ctx_RegEdi,
  345.         pContextRecord->ctx_RegEsi,
  346.         pContextRecord->ctx_RegEbp,
  347.         pContextRecord->ctx_SegCs,
  348.         pContextRecord->ctx_RegEip,
  349.         pContextRecord->ctx_SegSs,
  350.         pContextRecord->ctx_RegEsp,
  351.         pContextRecord->ctx_SegDs,
  352.         pContextRecord->ctx_SegEs,
  353.         pContextRecord->ctx_SegFs,
  354.         pContextRecord->ctx_SegGs
  355.       ) ;
  356.                      
  357.       PLONG Base = PLONG(pContextRecord->ctx_RegEbp) ;
  358.       while ( Base && ( *Base > LONG(Base) ) ) {
  359.         Log ( "  Calling function was at %08lX.", *(Base+1) ) ;
  360.         Base = PLONG(*Base) ;
  361.       } /* endwhile */
  362.  
  363.       ResourceString Exception ( Library, IDS_EXCEPTION ) ;
  364.       Debug ( HWND_DESKTOP, PCHAR(Exception), _fullpath(PCHAR(NULL),"MEMSIZE.LOG",0) ) ;
  365.  
  366.       DosExit ( EXIT_PROCESS, 0 ) ;
  367.    }
  368.  
  369.    if ( pExceptionReportRecord->ExceptionNum == XCPT_INTEGER_DIVIDE_BY_ZERO )
  370.    {
  371.       Log
  372.       (
  373.         "ABORT: An integer divide-by-zero error has occurred.\n"
  374.         "  The registers were as follows:\n"
  375.         "    AX:%08lX  BX:%08lX  CX:%08lX  DX:%08lX\n"
  376.         "    FL:%08lX  DI:%08lX  SI:%08lX  BP:%08lX\n"
  377.         "    CS:%08lX  IP:%08lX  SS:%08lX  SP:%08lX\n"
  378.         "    DS:%08lX  ES:%08lX  FS:%08lX  GS:%08lX",
  379.         pContextRecord->ctx_RegEax,
  380.         pContextRecord->ctx_RegEbx,
  381.         pContextRecord->ctx_RegEcx,
  382.         pContextRecord->ctx_RegEdx,
  383.         pContextRecord->ctx_EFlags,
  384.         pContextRecord->ctx_RegEdi,
  385.         pContextRecord->ctx_RegEsi,
  386.         pContextRecord->ctx_RegEbp,
  387.         pContextRecord->ctx_SegCs,
  388.         pContextRecord->ctx_RegEip,
  389.         pContextRecord->ctx_SegSs,
  390.         pContextRecord->ctx_RegEsp,
  391.         pContextRecord->ctx_SegDs,
  392.         pContextRecord->ctx_SegEs,
  393.         pContextRecord->ctx_SegFs,
  394.         pContextRecord->ctx_SegGs
  395.       ) ;
  396.                      
  397.       PLONG Base = PLONG(pContextRecord->ctx_RegEbp) ;
  398.       while ( Base && ( *Base > LONG(Base) ) ) {
  399.         Log ( "  Calling function was at %08lX.", *(Base+1) ) ;
  400.         Base = PLONG(*Base) ;
  401.       } /* endwhile */
  402.  
  403.       ResourceString Exception ( Library, IDS_EXCEPTION ) ;
  404.       Debug ( HWND_DESKTOP, PCHAR(Exception), _fullpath(PCHAR(NULL),"MEMSIZE.LOG",0) ) ;
  405.  
  406.       DosExit ( EXIT_PROCESS, 0 ) ;
  407.    }
  408.  
  409.    if ( pExceptionReportRecord->ExceptionNum == XCPT_INTEGER_OVERFLOW )
  410.    {
  411.       Log
  412.       (
  413.         "ABORT: An integer overflow has occurred.\n"
  414.         "  The registers were as follows:\n"
  415.         "    AX:%08lX  BX:%08lX  CX:%08lX  DX:%08lX\n"
  416.         "    FL:%08lX  DI:%08lX  SI:%08lX  BP:%08lX\n"
  417.         "    CS:%08lX  IP:%08lX  SS:%08lX  SP:%08lX\n"
  418.         "    DS:%08lX  ES:%08lX  FS:%08lX  GS:%08lX",
  419.         pContextRecord->ctx_RegEax,
  420.         pContextRecord->ctx_RegEbx,
  421.         pContextRecord->ctx_RegEcx,
  422.         pContextRecord->ctx_RegEdx,
  423.         pContextRecord->ctx_EFlags,
  424.         pContextRecord->ctx_RegEdi,
  425.         pContextRecord->ctx_RegEsi,
  426.         pContextRecord->ctx_RegEbp,
  427.         pContextRecord->ctx_SegCs,
  428.         pContextRecord->ctx_RegEip,
  429.         pContextRecord->ctx_SegSs,
  430.         pContextRecord->ctx_RegEsp,
  431.         pContextRecord->ctx_SegDs,
  432.         pContextRecord->ctx_SegEs,
  433.         pContextRecord->ctx_SegFs,
  434.         pContextRecord->ctx_SegGs
  435.       ) ;
  436.                      
  437.       PLONG Base = PLONG(pContextRecord->ctx_RegEbp) ;
  438.       while ( Base && ( *Base > LONG(Base) ) ) {
  439.         Log ( "  Calling function was at %08lX.", *(Base+1) ) ;
  440.         Base = PLONG(*Base) ;
  441.       } /* endwhile */
  442.  
  443.       ResourceString Exception ( Library, IDS_EXCEPTION ) ;
  444.       Debug ( HWND_DESKTOP, PCHAR(Exception), _fullpath(PCHAR(NULL),"MEMSIZE.LOG",0) ) ;
  445.  
  446.       DosExit ( EXIT_PROCESS, 0 ) ;
  447.    }
  448.  
  449.    return ( XCPT_CONTINUE_SEARCH ) ;
  450. }
  451.